<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style type="text/css">
    .box {
      margin-top: 100px;
      text-align: center;
    }

    #canvas {
      border: 1px solid #ccc;
    }
  </style>
</head>

<body>
  <div class="box">
    <canvas id="canvas" width="200" height="200"></canvas>
  </div>
</body>
<script type="text/javascript">
  // 备份
  /*
  function draw () {
    var canvas = document.getElementById('canvas')
    if (canvas.getContext) {
      var ctx = canvas.getContext('2d')
    }
  }
   */

  // 图形
  /*
  function draw() {
    var canvas = document.getElementById('canvas')
    if (canvas.getContext) {
      var ctx = canvas.getContext('2d')
      ctx.fillRect(25, 25, 100, 100)
      ctx.clearRect(45, 45, 60, 60)
      ctx.strokeRect(50, 50, 50, 50)
    }
  }
  */

  // 三角形
  /*
  function draw () {
    var canvas = document.getElementById('canvas')
    if (canvas.getContext) {
      var ctx = canvas.getContext('2d')
      ctx.beginPath()
      ctx.moveTo(75, 50)
      ctx.lineTo(100, 75)
      ctx.lineTo(100, 25)
      ctx.fill()
    }
  }
  */
  // 笑脸
  /*
  function draw() {
    var canvas = document.getElementById('canvas')
    if (canvas.getContext) {
      var ctx = canvas.getContext('2d')
      ctx.beginPath()
      // context.arc(x,y,r,sAngle,eAngle,counterclockwise);
      // x, y 表示圆心坐标
      ctx.arc(75, 75, 50, 0, Math.PI * 2, true);
      ctx.moveTo(110, 75)
      ctx.arc(75, 75, 35, 0, Math.PI, false);
      ctx.moveTo(65, 65);
      ctx.arc(60, 65, 5, 0, Math.PI * 2, true); // 左眼
      ctx.moveTo(95, 65);
      ctx.arc(90, 65, 5, 0, Math.PI * 2, true); // 右眼
      ctx.stroke();
    }
  }
  */
  // 2D对象
  /*
  function draw () {
    var canvas = document.getElementById('canvas')
    if (canvas.getContext) {
      var ctx = canvas.getContext('2d')
      var rectangle = new Path2D()
      rectangle.rect(10, 10, 50, 50)

      var circle = new Path2D()
      circle.moveTo(125, 35)
      circle.arc(100, 35, 25, 0, 2*Math.PI)

      ctx.strokeStyle = "orange"
      ctx.stroke(rectangle)

      ctx.fillStyle = "rgba(255,165,0,0.5)"
      ctx.fill(circle)
    }
  }
  */
  // 彩色圈图
  function draw() {
    var ctx = document.getElementById('canvas').getContext('2d');
    for (var i=0;i<6;i++){
      for (var j=0;j<6;j++){
        ctx.strokeStyle = 'rgb(0,' + Math.floor(255-42.5*i) + ',' +
                         Math.floor(255-42.5*j) + ')';
        ctx.beginPath();
        ctx.arc(12.5+j*25,12.5+i*25,10,0,Math.PI*2,true);
        ctx.stroke();
      }
    }
  }
  draw()
</script>

</html>
